package com.example.waterdispenserm2.mo.usbcong;


import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;

import com.example.waterdispenserm2.util.MyLog;
import com.google.gson.Gson;
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;
import com.hoho.android.usbserial.util.SerialInputOutputManager;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

    public class UsbCongInductionCookerDisplay {
        private Context context;
        private UsbSerialPort port;
        private SerialInputOutputManager serialIoManager;
        private final ExecutorService executor = Executors.newSingleThreadExecutor();
        private DisplayCallback callback;
        private int currentKeyCode = 0; // 当前按键值
        private boolean isConnected = false;
        private UsbDeviceConnection connection;

        // 段码表 (0-9, A-F)
        public static final byte[] SEGMENT_CODES = {
                (byte) 0x3F, // 0: 00111111
                (byte) 0x06, // 1: 00000110
                (byte) 0x5B, // 2: 01011011
                (byte) 0x4F, // 3: 01001111
                (byte) 0x66, // 4: 01100110
                (byte) 0x6D, // 5: 01101101
                (byte) 0x7D, // 6: 01111101
                (byte) 0x07, // 7: 00000111
                (byte) 0x7F, // 8: 01111111
                (byte) 0x6F, // 9: 01101111
                (byte) 0x77, // A: 01110111
                (byte) 0x7C, // B: 01111100
                (byte) 0x39, // C: 00111001
                (byte) 0x5E, // D: 01011110
                (byte) 0x79, // E: 01111001
                (byte) 0x71  // F: 01110001
        };

        // LED状态定义
        public static final byte LED_FRYING = (byte) 0x80;     // 炒菜  10000000
        public static final byte LED_PORRIDGE = (byte) 0x40;   // 煲粥  01000000
        public static final byte LED_HOTPOT = (byte) 0x20;     // 火锅  00100000
        public static final byte LED_HIGH_FIRE = (byte) 0x10;  // 武火  00010000
        public static final byte LED_LOW_FIRE = (byte) 0x08;   // 文火  00001000
        public static final byte LED_BOIL_WATER = (byte) 0x04; // 烧水  00000100
        public static final byte LED_STEAM = (byte) 0x02;      // 蒸煮  00000010
        public static final byte LED_STEW = (byte) 0x01;       // 煲汤  00000001

        // 按键定义
        public static final int KEY_NONE = 0;
        public static final int KEY_POWER = 1;
        public static final int KEY_TIMER = 2;
        public static final int KEY_DECREASE = 3;
        public static final int KEY_INCREASE = 4;
        public static final int KEY_FUNCTION = 5;
        public static final int KEY_START = 6;

        public interface DisplayCallback {
            void onDisplayUpdate(int thousands, int hundreds, int tens, int units, byte ledStatus);
            void onConnectionStatusChanged(boolean connected);
            void onError(String errorMessage);
            void onKeySent(int keyCode);
            void onDataReceived(byte[] data);
            void onDataSent(byte[] data);
        }

        public UsbCongInductionCookerDisplay(Context context, DisplayCallback callback) {
            this.context = context;
            this.callback = callback;
        }

        public boolean connectToDevice() {
            // 查找USB设备
            UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
            List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);

            if (availableDrivers.isEmpty()) {
                callback.onError("未找到USB转1-Wire适配器");
                return false;
            }

            // 获取第一个可用设备
            UsbSerialDriver driver = availableDrivers.get(0);
            UsbDevice device = driver.getDevice();
            if (!usbManager.hasPermission(device)) {
                callback.onError("无USB设备权限");
                return false;
            }

            try {
                // 打开连接
                connection = usbManager.openDevice(device);
                if (connection == null) {
                    callback.onError("无法打开USB连接");
                    return false;
                }

                port = driver.getPorts().get(0);
                port.open(connection);
                port.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);

                // 启动数据监听
                serialIoManager = new SerialInputOutputManager(port, new SerialInputOutputManager.Listener() {
                    @Override
                    public void onNewData(byte[] data) {
                        callback.onDataReceived(data);
                        processReceivedData(data);
                    }

                    @Override
                    public void onRunError(Exception e) {
                        callback.onError("通信错误: " + e.getMessage());
                    }
                });
                executor.submit(serialIoManager);

                isConnected = true;
                callback.onConnectionStatusChanged(true);
                return true;
            } catch (IOException e) {
                callback.onError("连接失败: " + e.getMessage());
                return false;
            }
        }

        public void disconnect() {
            if (serialIoManager != null) {
                serialIoManager.stop();
                serialIoManager = null;
            }
            if (port != null) {
                try {
                    port.close();
                } catch (IOException e) {
                    // 忽略关闭错误
                }
                port = null;
            }
            if (connection != null) {
                connection.close();
                connection = null;
            }
            isConnected = false;
            callback.onConnectionStatusChanged(false);
        }

        public void setCurrentKey(int keyCode) {
            this.currentKeyCode = keyCode;
        }

        private void processReceivedData(byte[] data) {
            // 主机发送协议帧：引导码 + 1byte识别码(0xA5) + 5byte显示码 + 1byte校验码 (共7字节)
            if (data.length < 7) {
                callback.onError("数据长度不足: " + data.length + "字节");
                return;
            }

            // 检查识别码
            if (data[0] != (byte) 0xA5) {
                callback.onError("无效的识别码: 0x" + String.format("%02X", data[0]));
                return;
            }

            // 计算校验和
            int checksum = 0;
            for (int i = 0; i < 6; i++) {
                checksum += data[i] & 0xFF;
            }
            checksum &= 0xFF;

            // 验证校验码
            if ((byte) checksum != data[6]) {
                callback.onError("校验失败: 计算值0x" + String.format("%02X", checksum)
                        + " 接收值0x" + String.format("%02X", data[6]));
                return;
            }

            // 解析显示数据
            int thousands = findSegmentIndex(data[1]); // 千位
            int hundreds = findSegmentIndex(data[2]);  // 百位
            int tens = findSegmentIndex(data[3]);      // 十位
            int units = findSegmentIndex(data[4]);     // 个位
            byte ledStatus = data[5];                  // LED状态

            // 更新显示
            callback.onDisplayUpdate(thousands, hundreds, tens, units, ledStatus);

            // 延时4ms后回复按键信息
            new Thread(() -> {
                try {
                    Thread.sleep(4);
                    sendKeyResponse();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }).start();
        }

        private int findSegmentIndex(byte segment) {
            for (int i = 0; i < SEGMENT_CODES.length; i++) {
                if (SEGMENT_CODES[i] == segment) {
                    return i;
                }
            }
            return 0; // 默认显示0
        }

        private void sendKeyResponse() {
            if (!isConnected || port == null) return;

            try {
                // 从机回复协议帧：1byte数据码 + 1byte校验码
                byte[] response = new byte[2];
                response[0] = (byte) currentKeyCode;
                response[1] = (byte) ~currentKeyCode; // 取反作为校验码
                MyLog.d("---发送--",new Gson().toJson(response));
                port.write(response, 100);
                callback.onDataSent(response);
                callback.onKeySent(currentKeyCode);

                // 重置按键值（避免重复发送）
                currentKeyCode = KEY_NONE;
            } catch (IOException e) {
                callback.onError("发送按键失败: " + e.getMessage());
            }
        }

        public static String getKeyName(int keyCode) {
            switch (keyCode) {
                case KEY_POWER: return "电量/电压";
                case KEY_TIMER: return "定时/预约";
                case KEY_DECREASE: return "减";
                case KEY_INCREASE: return "加";
                case KEY_FUNCTION: return "功能";
                case KEY_START: return "启动";
                default: return "无按键";
            }
        }

        public static String getSegmentName(byte segment) {
            for (int i = 0; i < SEGMENT_CODES.length; i++) {
                if (SEGMENT_CODES[i] == segment) {
                    return String.valueOf(i < 10 ? i : (char)('A' + i - 10));
                }
            }
            return "?";
        }

        public static String byteToBinaryString(byte b) {
            return String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
        }



}
